﻿using MaiPlanServer.API.Controllers.MasterData;
using MaiPlanServer.IServices.IWarehouseServives;
using MaiPlanServer.Model;
using MaiPlanServer.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace MaiPlanServer.API.Controllers.InventoryData
{
    /// <summary>
    /// 仓库设置
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class WarehouseSettingsController : ControllerBase
    {
        public WarehouseSettingsController(IWarehouseSettingsManageServices WsMs) 
        {
            this.WsMs = WsMs;
        }

        public IWarehouseSettingsManageServices WsMs { get; }
        /// <summary>
        /// 获取仓库设置表数据
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="WarehouseName"></param>
        /// <param name="Warehousecode"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(int PageIndex, int PageSize, string WarehouseName, string Warehousecode) {
            try
            {
                var list = await WsMs.GetArmazemAsync(PageIndex, PageSize, WarehouseName, Warehousecode);
                var count = await WsMs.GetCountAsync(WarehouseName);

                return Ok(BaseState.CreateInstance(200, "请求成功", list, count));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败ex"));
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromForm] wm_warehouse model)
        {
            try
            {
                model.Warehouse_id = Guid.NewGuid();
                model.CREATE_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                var result=await WsMs.AddAsync(model);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "新增成功"));
                }
                return Ok(BaseState.CreateInstance(200,"新增失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "新增失败，服务器异常，请联系管理员"));
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Put([FromForm] wm_warehouse model)
        {
            try
            {
                var result = await WsMs.UpdateAsync(model);

                return Ok(BaseState.CreateInstance(200, "修改成功"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "修改失败，服务器异常，请联系管理员"));
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Del(string Id)
        {
            try
            {
                var result = await WsMs.DellAsync(Id);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "删除失败，服务器异常，请联系管理员"));
            }
        }
        /// <summary>
        /// 删除多条
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete("DelAll")]
        public async Task<IActionResult> DelAll(string Id)
        {
            try
            {
                string[] ids = Id.Split(',');
                var result = await WsMs.DellAllAsync(ids);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "删除失败，服务器异常，请联系管理员"));
            }
        }

    }
}
